<?php
class User extends AppModel {

    var $name = 'User';
    var $actsAs = array('Multivalidatable');
    var $hasOne = 'Profile';
#    var $hasMany = array('Link');
    
    var $validate = array(
        'username' => array(
            'length' => array(
                'rule' => array('between', 6, 15),
                'message' => 'Your username must be between 6 and 15 characters long',
                'last' => true,
                'required' => true
            ),
            'alphanumeric' => array(
                'rule' => 'alphanumeric',
                'message' => 'Your username can only contain letters and numbers',
                'last' => true
            ),
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'Sorry, that username is already in use',
                'last' => true
            )
        ),
        'password' => array(
            'empty' => array(
                'rule' => 'notEmpty',
                'message' => 'Password must not be empty!',
                'required' => true,
            ), 
        ),
        'password_confirm' => array(
            'length' => array(
                'rule' => array('minLength', 8),
                'message' => 'Your password must be at least 8 characters long',
                'last' => true,
                'required' => true
            ),
            'compare' => array(
                'rule' => array('comparePasswords', 'password', true),
                'message' => 'Passwords do not match',
                'last' => true
            ) 
        )
    );
    
    var $validationSets = array(
        'updateToken' => array(
            'token' => array(
                'length' => array(
                    'rule' => array('maxLength', 3),
                    'message' => 'You cannot buy more than 100 tokens per purchase',
                    'last' => true,
                    'required' => true
                ),
                'numeric' => array(
                    'rule' => 'numeric',
                    'message' => 'Please enter a valid token',
                    'last' => true,
                )
            )
        )
    );
    
    function compareNewPwd($check = array(), $password = null) {
        foreach($check as $key => $value) {
            return $value === $this->data[$this->name][$password];
        }
        
    }
    
    function isDifferent($data) {
        $password = $this->field('password', array('User.id' => $this->id));
        return $password !== Security::hash($data['new_password'], null, true);
    }
    
    function current_password($data) {
        $password = $this->field('password', array('User.id' => $this->id));
        return $password === Security::hash($data['current_password'], null, true);
    }
    
    function comparePasswords($data, $password_field, $hashed = true)
    {
#        return $this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm']);
        $password = $this->data[$this->alias][$password_field];
        $keys = array_keys($data);
        $password_confirm = $hashed ? Security::hash($data[$keys[0]], null, true) : $data[$keys[0]];
        return $password === $password_confirm;
    }

    //Check the invitation code
#    function isValid($code) {
#        return ( $code['invitation_code'] == 'M1f59');
#    }


    /**
     * Generate a random pronounceable password
     */ 
    function generatePassword($length = 10) {
        // Seed
        srand((double) microtime()*1000000);

        $vowels = array('a', 'e', 'i', 'o', 'u');
        $cons = array('b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm', 'n',
        'p', 'r', 's', 't', 'u', 'v', 'w', 'tr',
        'cr', 'br', 'fr', 'th', 'dr', 'ch', 'ph',
        'wr', 'st', 'sp', 'sw', 'pr', 'sl', 'cl');

        $num_vowels = count($vowels);
        $num_cons = count($cons);

        $password = '';
        for ($i = 0; $i < $length; $i++){
            $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];
        }

        return substr($password, 0, $length);
    }
    
}
?>
